/*
  *  Copyright (C) 2011-2019 Project SkyFire <http://www.projectskyfire.org/>
  *  Copyright (C) 2008-2019 TrinityCore <http://www.trinitycore.org/>
  *  Copyright (C) 2005-2019 MaNGOS <https://getmangos.com/>
  *
  *  This program is free software; you can redistribute it and/or modify it
  *  under the terms of the GNU General Public License as published by the
  *  Free Software Foundation; either version 3 of the License, or (at your
  *  option) any later version.
  *
  *  This program is distributed in the hope that it will be useful, but WITHOUT
  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  *  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  *  more details.
  *
  *  You should have received a copy of the GNU General Public License along
  *  with this program. If not, see <http://www.gnu.org/licenses/>.
  */

#ifndef _REALMLIST_H
#define _REALMLIST_H

#include "Common.h"

#include <ace/Singleton.h>
#include <ace/Null_Mutex.h>
#include <ace/INET_Addr.h>

enum RealmFlags
{
   REALM_FLAG_NONE         = 0x00,
   REALM_FLAG_INVALID      = 0x01,
   REALM_FLAG_OFFLINE      = 0x02,
   REALM_FLAG_SPECIFYBUILD = 0x04,                         // client will show realm version in RealmList screen in form "RealmName (major.minor.revision.build)"
   REALM_FLAG_UNK1         = 0x08,
   REALM_FLAG_UNK2         = 0x10,
   REALM_FLAG_RECOMMENDED  = 0x20,                         // client checks pop == 600f
   REALM_FLAG_NEW          = 0x40,                         // client checks pop == 200f
   REALM_FLAG_FULL         = 0x80                          // client checks pop == 400f
};

// Storage object for a realm
struct Realm
{
    ACE_INET_Addr ExternalAddress;
    ACE_INET_Addr LocalAddress;
    ACE_INET_Addr LocalSubnetMask;
    std::string name;
    uint8 icon;
    RealmFlags flag;
    uint8 timezone;
    uint32 m_ID;
    AccountTypes allowedSecurityLevel;
    float populationLevel;
    uint32 gamebuild;
};

/// Storage object for the list of realms on the server
class RealmList
{
public:
    typedef std::map<std::string, Realm> RealmMap;

    RealmList();
    ~RealmList() {}

    void Initialize(uint32 updateInterval);

    void UpdateIfNeed();

    void AddRealm(const Realm NewRealm) {m_realms[NewRealm.name] = NewRealm; }

    RealmMap::const_iterator begin() const { return m_realms.begin(); }
    RealmMap::const_iterator end() const { return m_realms.end(); }
    uint32 size() const { return m_realms.size(); }

private:
    void UpdateRealms(bool init = false);
    void UpdateRealm(uint32 id, const std::string& name, ACE_INET_Addr const& address, ACE_INET_Addr const& localAddr, ACE_INET_Addr const& localSubmask, uint8 icon, RealmFlags flag, uint8 timezone, AccountTypes allowedSecurityLevel, float popu, uint32 build);

    RealmMap m_realms;                                  ///< Internal map of realms
    uint32   m_UpdateInterval;
    time_t   m_NextUpdateTime;
};

#define sRealmList ACE_Singleton<RealmList, ACE_Null_Mutex>::instance()

#endif
